﻿using Model;
using Presenters.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AirlinePilotAcademy_WPF.UI_Administrator.Subject
{
    /// <summary>
    /// Lógica de interacción para SearchCourse_Horary.xaml
    /// </summary>
    public partial class SearchCourse_Horary : Window
    {
        private Model.Classroom classroom;
        private TextBox textBox;
        public bool execute { get; set; }


        public SearchCourse_Horary()
        {
            InitializeComponent();

            this.DataGrid_Result.ItemsSource = DataAccess.ClassroomDataAccess.getClassroomsByCode(this.TextBox_Filter.Text);
        }

        public SearchCourse_Horary(Model.Classroom isInHoraryObject, TextBox textBox)
        {
            InitializeComponent();

            execute = false;
            this.TextBox_Filter.Text = textBox.Text;
            this.classroom = isInHoraryObject;
            this.textBox = textBox;

            classroom = DataAccess.ClassroomDataAccess.getClassroomByCode(this.TextBox_Filter.Text);
            if (classroom == null)
            {
                execute = true;
            }

            this.DataGrid_Result.ItemsSource = DataAccess.ClassroomDataAccess.getClassroomsByCode(this.TextBox_Filter.Text);
        }

        private void AddClassroom()
        {
            if (String.IsNullOrEmpty(this.TextBox_Filter.Text))
            {
                this.textBox.Text = "";
                this.Close();
            }
            else
            {
                this.classroom = DataAccess.ClassroomDataAccess.getClassroomByCode(this.TextBox_Filter.Text);
                if (classroom == null)
                {
                    MessageBoxResult result;
                    result = MessageBox.Show("El aula no existe, intente de nuevo", "Mensaje");
                }
                else
                {
                    classroom.code = classroom.code.Replace(" ", "");
                    this.textBox.Text = TextBox_Filter.Text;
                    this.Close();
                }
            }

        }

        private void DataGrid_Click_SelectedClassroom(object sender, MouseButtonEventArgs e)
        {
            if (this.DataGrid_Result.SelectedIndex >= 0)
            {
                Model.Classroom aux_classroom = (Model.Classroom)this.DataGrid_Result.SelectedItem;
                this.TextBox_Filter.Text = aux_classroom.code;
                this.classroom = aux_classroom;
            }
        }

        private void TextBox_Clicked_AddClassroom(object sender, RoutedEventArgs e)
        {
            AddClassroom();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.DataGrid_Result.ItemsSource = DataAccess.ClassroomDataAccess.getClassroomsByCode(this.TextBox_Filter.Text);
        }

        private void TextBox_Click_AddClassroom(object sender, RoutedEventArgs e)
        {
            if (this.DataGrid_Result.CurrentCell != null)
            {
                Model.Classroom aux_classroom = (Model.Classroom)this.DataGrid_Result.CurrentCell.Item;
                this.TextBox_Filter.Text = aux_classroom.code;
                this.classroom = aux_classroom;
            }
        }
    }
}
